package com.macrounion.atv.processor;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.macrounion.atv.service.dto.resp.SpotListRespDto;
import com.macrounion.atv.service.dto.resp.SpotRespDto;
import com.macrounion.atv.service.entity.Device;
import com.macrounion.atv.service.entity.ext.SpotExt;
import com.macrounion.atv.service.service.DeviceService;
import com.macrounion.base.service.dto.BaseRespDto;
import com.macrounion.base.service.dto.PageRespDto;
import com.macrounion.base.service.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.macrounion.atv.service.entity.Spot;
import com.macrounion.atv.service.service.SpotService;
import com.macrounion.base.processor.BaseProcessor;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class SpotProcessor extends BaseProcessor<Spot, Spot, SpotService, Spot> {

    @Autowired
    private DeviceService deviceService;

    public List<Spot> getSpotByParentId(String parentId){
        return service.getSpotByParentId(parentId);
    }

    public List<SpotRespDto> getTreeSpot(String parentSpotId,Integer isLoadDevice,Integer isLoadPing){
        return service.getTreeSpot(parentSpotId,isLoadDevice,isLoadPing);
    }

    public List<Spot> getAllSpot(String curSpotId){
        List<Spot> allSpot=service.getAllSpot();
        if(!StringUtils.isEmpty(curSpotId)){
            List<Spot>  childrenList=service.getChildrenSpotById(curSpotId);
            if(!CollectionUtils.isEmpty(childrenList)){
                childrenList.forEach(p->{
                    allSpot.removeIf(q->q.getId().equals(p.getId()));
                });
            }
            allSpot.removeIf(q->q.getId().equals(curSpotId));
        }

        return allSpot;
    }

    @Override
    public PageRespDto<Spot> requestPage(Spot e) {
        return super.requestPage(e);
    }

    public PageRespDto<SpotListRespDto> getSpotList(SpotExt spot){
        PageRespDto<SpotListRespDto> pageResp = new PageRespDto();


//        List<String> ids=Lists.newArrayList();
//        List<Spot> children=service.getChildrenSpotById(spot.getParentId());
//        if(!CollectionUtils.isEmpty(children)){
//            ids=children.stream().map(Spot :: getId).collect(Collectors.toList());
//        }
//        spot.setSpotIds(ids);

        List spotIds=Lists.newArrayList();

        if(CollectionUtils.isEmpty(spot.getSpotIds()) && !StringUtils.isEmpty(spot.getParentId())){
            spotIds.add("");
            spot.setSpotIds(spotIds);
        }
        PageHelper.startPage(spot.getPageInfo().getPageIndex(),spot.getPageInfo().getPageSize());
        Page<SpotListRespDto> pm=(Page<SpotListRespDto>) service.getSpotListBySpot(spot);
        BeanUtils.copyProperties(pm, pageResp);
        pageResp.setDataList(pm.getResult());
        return pageResp;
    }

    @Override
    public Spot save(Spot reqDto) {
        //检查站点名称不能重复
        int findCount=service.findCount(reqDto);
        if(findCount>0)
            throw new BusinessException(201,"站点名称已经存在");
        if(!StringUtils.isEmpty(reqDto.getParentId())){
            Spot parentSpot=service.findById(reqDto.getParentId());
            if(parentSpot==null)
                throw new BusinessException(202,"父站点不存在");
        }
        return super.save(reqDto);
    }

    @Override
    public boolean removeById(String id) {
        //站点下有站点或设备不让删除
        List<Device> deviceList=deviceService.getDeviceBySpotId(id);
        List<Spot> spotList=service.getSpotByParentId(id);

        if(deviceList!=null && deviceList.size()>0
        || spotList!=null && spotList.size()>0)
            throw new BusinessException(201,"站点有下级站点或设备，不能删除");

        return super.removeById(id);
    }

    public List<Spot> getChildrenById(String parentId){
        return service.getChildrenSpotById(parentId);
    }
}